//=======================================================================
//功能: 			JavaScript mainCw
//author:		岳静
//e-mail: 		yuejing0129@126.com
//QQ:			503490146
//date: 		2012-11-20
//version: 		1.0
//=======================================================================
$(function() {
	findCwUser(1);
});

//获取同城用户信息
var cwUserInfoPage = new Page("cwUserInfoPage", findCwUser, "bodyInfo", "bodyInfoPage");
cwUserInfoPage.size = 15;
cwUserInfoPage.pageSizeArr = [10, 15, 20];
//处理Page标签的函数
function findCwUser(_page) {
	if(_page != undefined) {
		cwUserInfoPage.page = _page;
	}
	var _data = {
			"page" : cwUserInfoPage.page,
			"size" : cwUserInfoPage.size
	};
	if(cwUserInfoPage.typeid != undefined) {
		_data["typeid"] = cwUserInfoPage.typeid;
	}

	jQuery.ajax({
		url : webroot + "/cw/json/findByUserInfo.shtml",
		data : _data,
		type : "POST",
		dataType : "json",
		async : true,
		beforeSend: function(){
			//提交处理的函数
			cwUserInfoPage.pageBeforeSend("<li>加载同城用户信息中...</li>");
			loading("加载同城用户信息中...");
		},
		error : function(json){
			cwUserInfoPage.pageError("<li>加载同城用户信息出错了~</li>");
			unloading();
		},
		//返回的是一个对象
		success : function(json){
			if(json.result === "success") {
				//处理数据列表的行信息
				function getResult(obj) {
					var result = '<li>'
						+'<div class="pbfloatleft">'
						+'<img width="55" height="55" src="' + obj.img + '">'
						+'</div>'
						+'<div class="pbfloatright">'
						+'<p class="pbtitle"><a href="http://weibo.com/u/' + obj.username + '" target="_banlk" title="' + obj.nickname + '">' + obj.nickname + '</a></p>'
						+'<p><img src="' + webroot + '/resources/images/' + (obj.gender === '男' ? 'user_man.png' : 'user_woman.png') + '" alt="' + obj.gender + '" title="' + obj.gender + '"/>&nbsp;';
					if(obj.verified === 'true') {
						result += '<img src="' + webroot + '/resources/images/weibo/weibo_v_org.png" alt="已V认证" title="已V认证"/>';
					}
					result += '</p><p title="' + obj.location + '">' + obj.location + '</p></div>'
					+'<br clear="all"/>'
					+'<p class="pboperation">';

					//是否关注
					if(obj.isfa === 1) {
						result += '<a class="btngray" href="javascript:void(0);">已关注</a>&nbsp;';
					} else {
						result += '<a class="btngreen" href="javascript:void(0);" onclick="mainCw.attentionUser(this, \'' + obj.username + '\')">+关注</a>&nbsp;';
					}
					//是否互粉
					/*if(obj.isfamp === 1) {
								result += '<a class="btngray" href="javascript:void(0);" onclick="mainPb.sureFaOutcast(this, ' + obj.id + ', \'' + obj.username + '\');">已互粉</a>&nbsp;';
							} else {
								result += '<a class="btngreen" href="javascript:void(0);" onclick="mainPb.sureFaOutcast(this, ' + obj.id + ', \'' + obj.username + '\');">+互粉</a>&nbsp;';
							}*/
					result += '</p></li>';
					return result;
				}
				cwUserInfoPage.pageOperate(json.data, { resultFn : getResult, dataNull : "<li>没有查找到同城用户的数据!</li>", pageFootStyle : cwUserInfoPage.pageStyle.defaultStyle });
			}
			else if(json.result === "error_weibo") {
				alert(json.weiboMsg);
			}
			else if(json.result === "error") {
				alert("请求操作, 系统出现异常!");
			}
			unloading();
		}
	});
}

var mainCw = {

		/**
		 * 关注用户
		 * @param _this			点击的对象
		 * @param _uids			关注的用户的ID[133223,324324,]
		 */
		attentionUser : function(_this, _uids) {
			if(_this && $(_this).html() === '已关注') {
				return;
			}
			jQuery.ajax({
				url : webroot + "/cw/json/attentionUser.shtml",
				data : {
					"uids" : _uids
				},
				type : "POST",
				dataType : "json",
				async : true,
				beforeSend: function(){
					//提交处理的函数
					loading("关注中...");
				},
				error : function(json){
					alert("关注出错了~");
					unloading();
				},
				//返回的是一个对象
				success : function(json){
					var _faBool = false;
					if(json.result === "error_isopencwmp") {
						alert('请您开启同城互粉功能!&nbsp;<a href="#" onclick="comm.openCwMp(0);">[开启同城互粉]</a>');
					}
					else if(json.result === "success") {
						alert('关注用户成功!');
						_faBool = true;
					}
					else if(json.result === "error_weibo") {
						alert(json.errorWeiboMsg);
						if(json.errorWeiboCode === 20506) {
							_faBool = true;
						}
					}
					else if(json.result === "error") {
						alert('请求操作, 系统出现异常!');
					}
					if(_this != undefined && _faBool) {
						$(_this).attr('class', 'btngray');
						$(_this).html('已关注');
					}
					unloading();
				}
			});
		}
};